﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using Huazhu.PMS.Framework.Core;
using Huazhu.PMS.Framework.Organization;

namespace Huazhu.PMS.Framework
{
    internal class SecurityCache
    {
        #region 静态字段
        //锁对象
        private static object ayncLock = new object();
        private static SecurityCache _instance;
        private static readonly ReaderWriterLockSlim readerWriterLock = new ReaderWriterLockSlim();
        #endregion

        private SecurityCache()
        {
        }
        /// <summary>
        /// 单例
        /// </summary>
        /// <returns></returns>
        public static SecurityCache GetInstance()
        {
            if (_instance == null)
            {
                lock (ayncLock)
                {
                    if (_instance == null)
                    {
                        SecurityCache cache = new SecurityCache();                       
                        _instance = cache;
                        _instance.Init();
                        System.Threading.Thread.MemoryBarrier();
                    }
                }
            }
            return _instance;
        }


        private List<RulePage> control_rule_subcontrol_cache = new List<RulePage>();
        /// <summary>
        /// 页面-功能-控件名缓存
        /// </summary>
        internal List<RulePage> Control_Rule_SubControl_Cache
        {
            get
            {
                readerWriterLock.EnterReadLock();
                try
                {
                    return control_rule_subcontrol_cache;
                }
                finally
                {
                    readerWriterLock.ExitReadLock();
                }
            }            
        }

        private Dictionary<string, string> rule_role_cache = new Dictionary<string, string>();
        private IDictionary<string, Int64> rule_roleToken_cache = new Dictionary<string, long>();
        private IDictionary<string, Int64> role_roleToken_cache = new Dictionary<string, Int64>();
        private OrgTree ortTree_cache = new OrgTree();

        /// <summary>
        /// 角色
        /// </summary>
        internal IDictionary<string, Int64> RoleRoleTokenCache
        {
            get
            {
                readerWriterLock.EnterReadLock();
                try
                {
                    return role_roleToken_cache;
                }
                finally
                {
                    readerWriterLock.ExitReadLock();
                }
            }
        }

        /// <summary>
        /// 规则-角色
        /// </summary>
        internal IDictionary<string, Int64> RuleRoleTokenCache
        {
            get
            {
                readerWriterLock.EnterReadLock();
                try
                {
                    return rule_roleToken_cache;
                }
                finally
                {
                    readerWriterLock.ExitReadLock();
                }
            }
        }
        
        /// <summary>
        /// 角色数量
        /// </summary>
        internal int RolesAmount
        {
            get
            {
                readerWriterLock.EnterWriteLock();
                try
                {
                    if (RoleRoleTokenCache != null)
                        return RoleRoleTokenCache.Count;
                    else
                        return 0;
                }
                finally
                {
                    readerWriterLock.ExitWriteLock();
                }
            }
        }

        /// <summary>
        /// 功能-角色缓存 
        /// </summary>
        internal Dictionary<string, string> RuleRolesCache
        {
            get
            {
                readerWriterLock.EnterReadLock();
                try
                {
                    return rule_role_cache;
                }
                finally
                {
                    readerWriterLock.ExitReadLock();
                }
            }           
        }

        internal OrgTree OrganizationTree
        {
            get
            {
                readerWriterLock.EnterReadLock();
                try
                {
                    return OrgTreeProvider.GetOrganizationTree();
                }
                finally
                {
                    readerWriterLock.ExitReadLock();
                }
            }
        }

        /// <summary>
        /// 填充数据库数据进缓存
        /// </summary>
        /// <returns>初始化是否成功</returns>
        private void Init()
        {
            //control_rule_subcontrol_cache = new RulePageDataAccess().GetRulePages();
            //List<RoleInfo> roleList = new RoleDataAccess().GetRoles();
            //int count = 0;
            //foreach (RoleInfo role in roleList)
            //{
            //    Int64 token = ((Int64)1) << count++;
            //    role_roleToken_cache.Add(role.RoleID, token);
            //}
            //List<RuleRoles> ruleRolesList = new RuleRolesDataAccess().GetRuleRoles();
            //foreach (RuleRoles ruleRole in ruleRolesList)
            //{
            //    if (!rule_role_cache.ContainsKey(ruleRole.RuleID))
            //    {
            //        rule_role_cache.Add(ruleRole.RuleID, ruleRole.Expression);
            //        string[] roles = RuleParser.SplitExpression(ruleRole.Expression);
            //        Int64 token = 0;
            //        foreach (string role in roles)
            //        {
            //            token |= role_roleToken_cache[role];
            //        }
            //        rule_roleToken_cache.Add(ruleRole.RuleID, token);
            //    }
            //}         
        }
      

       
    }
}
